<script setup>
import { clearAllFromLocalStorage, getFromLocalStorage } from '@/utils/store-data';
import { ArrowRight, Setting }                    from '@element-plus/icons-vue';
import { ref }                                    from 'vue';
import { useRouter } from 'vue-router';


const router = useRouter()
// refs
const isCollapse = ref(false)
const AsideWidth = ref('200px')
const avatarURL  = ref(getFromLocalStorage('current-user').avatar || '@/assets/images/logo.png')

// constant
const CollapseWidth = '64px'
const ExpandWidth   = '200px'

// methods
const handleFullScreen = () => {        document.documentElement.requestFullscreen()       ; }
const handleFold       = () => { isCollapse.value = true ; AsideWidth.value = CollapseWidth; }
const handleExpand     = () => { isCollapse.value = false; AsideWidth.value = ExpandWidth  ; }


const handleLogout = () => {
  clearAllFromLocalStorage()
  router.push({ name: 'Login' })
}
</script>

<template>
  <div class="content">
    <el-container>
      <!-- Aside -->
      <el-aside class="aside" :width="AsideWidth">
        <!-- Aside Header -->
        <div class="aside-header"><img src="@/assets/images/logo1.png" alt="logo"><span class="logo-title" v-if="!isCollapse">管理系统</span></div>
        <!-- Menu -->
        <el-menu class="menu" background-color="#001529" active-text-color="#fff" :collapse-transition="false" text-color="rgba(255,255,255,0.65)" :collapse="isCollapse" router>
          <!-- 折叠后图标消失 -> 插槽包裹span,不包裹el-icon -->
          <el-menu-item index="/"><el-icon><Setting /></el-icon><template #title><span>系统首页 </span></template></el-menu-item>
          <el-sub-menu>
            <template #title><el-icon><Edit /></el-icon><span>  系统管理  </span></template>
            <el-menu-item index="/user">  <el-icon><Edit /></el-icon>用户信息</el-menu-item>
            <el-menu-item index="/news">  <el-icon><Edit /></el-icon>新闻信息</el-menu-item>
            <el-menu-item index="/notice"><el-icon><Edit /></el-icon>系统公告</el-menu-item>
            <el-menu-item index="/logs">  <el-icon><Edit /></el-icon>系统日志</el-menu-item>
            <el-menu-item index="/charts"><el-icon><Edit /></el-icon>数据统计</el-menu-item>
            <el-menu-item index="/orders"><el-icon><Edit /></el-icon>订单管理</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- Header -->
        <el-header class="header">
          <!-- Header Left -->
          <el-icon v-if="!isCollapse" class="icon26" @click="handleFold">   <Fold /> </el-icon>
          <el-icon v-if="isCollapse"  class="icon26" @click="handleExpand"><Expand /></el-icon>
          <el-breadcrumb :separator-icon="ArrowRight" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">主页</el-breadcrumb-item>
            <el-breadcrumb-item>用户信息</el-breadcrumb-item>
            <el-breadcrumb-item>数据统计</el-breadcrumb-item>
          </el-breadcrumb>
          <!-- Header Right -->
          <div class="header-right">
            <el-icon class="fullscreen icon26" @click="handleFullScreen"><FullScreen/></el-icon>
            <el-dropdown placement="bottom" trigger="click">
              <span class="el-dropdown-link"><img :src="avatarURL" alt="avatar" class="avatar"></span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>个人信息</el-dropdown-item>
                  <el-dropdown-item>修改密码</el-dropdown-item>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                  <el-dropdown-item disabled>禁用</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <span class="username">Admin</span>
          </div>
        </el-header>
        <!-- Main -->
        <el-main class="main"><RouterView /></el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style lang="scss">
.icon26{ font-size: 26px; }
.content {
  .aside {
    background-color: #001529;
    min-height: 100vh;
    transition: width .3s;
    box-shadow: 2px 0 6px rgba(0, 21, 41, .35);
    .aside-header {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 60px;
      img { width: 40px; height: 40px; }
      .logo-title {
        margin-left: 5px;
        font-size: 20px;
        color: white;
      }
    }
  }
  .menu {
    border: none;
    .el-menu-item { padding-left: 45px; }
  }
  .header {
    box-shadow: 2px 0 6px rgba(0, 21, 41, .35);
    display: flex;
    align-items: center;
    .breadcrumb{ margin-left: 20px; }
    .header-right {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      .fullscreen {
        margin-right: 10px;
      }
      .avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        margin: 0 5px;
      }
      .username {
        margin-left: 10px;
      }
    }
  }
  .main {}
}
</style>


